package readxml;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

public class DomDocument {
	
	public static void copyDocument(Document source, Document target){
	    Node node = target.importNode(source.getDocumentElement(), true);
	    target.getDocumentElement().appendChild(node);
	  }
	
	public static Document load(String xml) throws Exception {
	    DocumentBuilder builder = getDocumentBuilder();         
	    Document document = builder.parse(new InputSource(new StringReader(xml)));
	    return document;
	}
	
	
	public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
	    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
	    builderFactory.setNamespaceAware(true);
	    DocumentBuilder builder = builderFactory.newDocumentBuilder();
	    return builder;
	}

	
	public static Document createDocument(String rootQName) throws DOMException {
	      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	      try {
	          return factory.newDocumentBuilder ().getDOMImplementation ().createDocument (null, rootQName, null);
	      } catch (ParserConfigurationException ex) {
	          throw (DOMException)new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot create parser").initCause(ex); // NOI18N
	      }
	  }
	  
	
	
	
}
